-
Notifications
You must be signed in to change notification settings - Fork 5
feat(HTML)!: HTML class no longer inherits from str
#86
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
If a non-HTML value is added to an HTML value, the non-HTML value is escaped Fixes #15
…ngs of the tagified values
| TagT = TypeVar("TagT", bound="Tag") | ||
|
|
||
| TagAttrValue = Union[str, float, bool, None] | ||
| TagAttrValue = Union[str, float, bool, "HTML", None] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given parity for other large union objects (TagNode and TagChild), I do not believe we should make a is_tag_attr_value() method as consolidate_attrs() should internally handle it.
For now, we will skip making this helper method.
| """ | ||
|
|
||
| def tagify(self) -> "TagList | Tag | MetadataNode | str": ... | ||
| def tagify(self) -> "TagList | Tag | MetadataNode | str | HTML": ... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New HTML() support!
| TagT = TypeVar("TagT", bound="Tag") | ||
|
|
||
| TagAttrValue = Union[str, float, bool, None] | ||
| TagAttrValue = Union[str, float, bool, "HTML", None] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New HTML() support!
| # TagAttrDict class | ||
| # ============================================================================= | ||
| class TagAttrDict(Dict[str, str]): | ||
| class TagAttrDict(Dict[str, "str | HTML"]): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New HTML() support!
|
|
||
| @staticmethod | ||
| def _normalize_attr_value(x: TagAttrValue) -> Optional[str]: | ||
| def _normalize_attr_value(x: TagAttrValue) -> str | HTML | None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New HTML() support!
| return False | ||
|
|
||
| def add_style(self: TagT, style: str, *, prepend: bool = False) -> TagT: | ||
| def add_style(self: TagT, style: str | HTML, *, prepend: bool = False) -> TagT: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New HTML() support!
| handler(value) | ||
| elif hasattr(value, "_repr_html_"): | ||
| handler(HTML(value._repr_html_())) # pyright: ignore | ||
| elif isinstance(value, ReprHtml): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use isinstance() rather than look for the key, which isinstance performs
| ) -> tuple[TagAttrs, list[TagChildT]]: ... | ||
|
|
||
|
|
||
| def consolidate_attrs( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
new helper method (ported from py-shiny)
| """ | ||
|
|
||
|
|
||
| def is_tag_node(x: object) -> TypeIs[TagNode]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New helper method
| return isinstance(x, (Tagifiable, MetadataNode, ReprHtml, str, HTML)) | ||
|
|
||
|
|
||
| def is_tag_child(x: object) -> TypeIs[TagChild]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New helper method
| if isinstance(item, (int, float)): | ||
| result[i] = str(item) | ||
| elif not isinstance(item, (Tagifiable, Tag, MetadataNode, ReprHtml, str)): | ||
| elif not is_tag_node(item): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using new helper method
Co-authored-by: Winston Chang <[email protected]>
Breaking changes
HTMLno longer inherits fromstr. It now inherits fromcollections.UserString. This was done to avoid confusion betweenstrandHTMLobjects. (feat(HTML)!:HTMLclass no longer inherits fromstr#86)TagandTagList's method.get_html_string()now both returnstrinstead ofHTML. (feat(HTML)!:HTMLclass no longer inherits fromstr#86)Strings added to
HTMLobjects, now returnHTMLobjects. E.g.HTML_value + str_valueandstr_value_ + HTML_valueboth returnHTMLobjects. To maintain astrresult, callstr()on yourHTMLobjects before adding them to strings. (feat(HTML)!:HTMLclass no longer inherits fromstr#86)New features
Exported
ReprHtmlprotocol class. If an object has a_repr_html_method, then it is of instanceReprHtml. (feat(HTML)!:HTMLclass no longer inherits fromstr#86)Exported
is_tag_node()andis_tag_child()functions that utilizetyping.TypeIsto narrowTagNodeandTagChildtype variables, respectively. (feat(HTML)!:HTMLclass no longer inherits fromstr#86)Exported
consolidate_attrs(*args, **kwargs)function. This function will combine theTagAttrs(supplied in*args) withTagAttrValues(supplied in**kwargs) into a singleTagAttrsobject. In addition, it will also return all*argsthat are not dictionary as a list of unalteredTagChildobjects. (feat(HTML)!:HTMLclass no longer inherits fromstr#86)The
Tagmethod.add_style(style=)added support forHTMLobjects in addition tostrvalues. (feat(HTML)!:HTMLclass no longer inherits fromstr#86)Benefits:
strtype and anHTMLtype.Related py-shiny PRs in prep for this PR:
htmltools.HTMLno longer inheriting fromstrpy-shiny#1690